<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="pub/top_list2.jsp"%>   

<%
String names = (String) request.getSession().getAttribute("name");
String username=request.getParameter("username");
String tele=request.getParameter("tele");
String province=request.getParameter("province");
String city=request.getParameter("city");
String distric=request.getParameter("distric");
String address=request.getParameter("address");
int state=Integer.valueOf(request.getParameter("state"));
int id=Integer.valueOf(request.getParameter("l"));
%>
<script>
$(document).ready(function() {	
	$("#username").val("<%=username%>");
	$("#tele").val("<%=tele%>");
	$("#province").val('<%=province%>');
	$("#city").val('<%=city%>');
	$("#distric").val('<%=distric%>');
	$("#address").val('<%=address%>');
	var state=<%=state%>;
	
	if(state==1){
		$(".checkboxa label").addClass("checkd");
	}else{
		$(".checkboxa label").removeClass("checkd");
	}
});

function fun2(){
	var userid='<%=names%>'
	if($(".checkboxa label").hasClass('checkd')){
		document.getElementById("state2").value=1;
		$.ajax({
			type:"post",
			url:"stateAjax.jsp",
			dataType:"text",
			async : false,
			cache : false,
			data:"userid="+userid,
			success:function(){
				alert("您已将当前地址设置为默认地址")
			},
			error:function(e){
				console.log(e)
			}
		});
	}else{
		document.getElementById("state2").value=0;
	}
}
function shanchu(){
	alert("11111")
	var userid='<%=names%>'
	var dzbh=<%=id%>;
	$.ajax({
		type:"post",
		url:"dizhiAjax.jsp",
		dataType:"text",
		async : false,
		cache : false,
		data:"userid="+userid+"&id="+dzbh,
		success:function(){
			alert("您已删除该地址");
			window.location.href="addres.jsp";
		},
		error:function(e){
			console.log(e)
		}
	});
}

function baocun(){
	var username=document.getElementById("username").value;
	var tele=document.getElementById("tele").value;
	var province=document.getElementById("province").value;
	var city=document.getElementById("city").value;
	var distric=document.getElementById("distric").value;
	var address=document.getElementById("address").value;
	var state=document.getElementById("state2").value;
	console.log(state)
	var id=<%=id%>;
	$.ajax({
		type:"post",
		url:"xgdzAjax.jsp",
		dataType:"text",
		async : false,
		cache : false,
		data:"username="+username+"&tele="+tele+"&province="+province+"&city="+city+"&distric="+distric+"&address="+address+"&state="+state+"&id="+id,
		success:function(){
			alert("您的地址信息已修改成功");
			window.location.href="addres.jsp";
		},
		error:function(e){
			console.log(e)
		}
	});
		
	
}
</script>

<body>
	<header class="top-header fixed-header">
		<a class="icona" href="addres.jsp">
				<img src="images/left.png"/>
			</a>
		<h3>修改地址</h3>			
	</header>
	
	<div class="contaniner fixed-conta">
		<form action="addres.jsp" method="post" class="change-address" id="save">
			<ul>
				<li>
					<label class="addd">收货人：</label>
					<input type="text" id="username" value="" required="required"/>
				</li>
				<li>
					<label class="addd">手机号：</label>
					<input type="tel" id="tele" value="" required="required"/>
				</li>
				<li>
					<label class="addd">所在地区：</label>
					<input type="text" id="province" value="" style="width:74px" required="required"/>
					<input type="text" id="city" value="" style="width:74px" required="required"/>
					<input type="text" id="distric" value="" style="width:74px" required="required"/>
				</li>
				<li>
					<label class="addd">详细地址：</label>
					<textarea id="address" required="required"></textarea>
				</li>
			</ul>
			
			<ul>
				<li class="checkboxa">
					<input type="text" name="state2" id="state2" value="0"  style="display:none"/>
					<input type="checkbox" id="state" value="0" onclick="fun2()"/>
					<label class="check" for="state" >设置为默认地址</label>
				</li>
			</ul>
			<ul>
				<li>
					<a onclick="shanchu()"><h3>删除此地址</h3></a>
				</li>
			</ul>
			<input type="button" value="保存" onclick="baocun()" style="width: 90%; margin: 3% 5%;padding: 3% 0;font-size: 1.5em;color: #fff;background-color: #db3752;border: none;border-radius: 0.4em;"/>
		</form>
	</div>
	
	<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		$(".checkboxa label").on('touchstart',function(){
			if($(this).hasClass('checkd')){
				$(".checkboxa label").removeClass("checkd");
			}else{
				$(".checkboxa label").addClass("checkd");
			}
		})
	</script>
	

</body>
</html>